from odoo import fields,models,api,_
from odoo.exceptions import UserError,ValidationError,Warning,RedirectWarning,MissingError,AccessError,AccessDenied
class ResUsers(models.Model):
    _inherit = "res.users"
    department_ids = fields.Many2many(comodel_name='hr.department', string='权限管理部门')
    default_department =fields.Many2one(comodel_name='hr.department', string='默认管理单位')
    current_id = fields.Integer(compute='get_current_uid', string='当前登录用户id')
    def get_current_uid(self):
        if self.env.context.get('uid', False):
            self.current_id = self.env.context.get('uid', False)
        else:
            self.current_id = False
    @api.model
    def create(self,vals):
        vals['lang'] = "zh_CN"
        vals['tz'] = 'Asia/Shanghai'
        passwd = self.env['ir.config_parameter'].sudo().get_param("user_default_password", '123456')
        vals['password'] = passwd
        new_record = super(ResUsers, self).create(vals)
        return new_record

    @api.model
    def action_get_user(self, uid):

        return {
            'type': 'ir.actions.client',
            'tag': 'change_password', #在change_passwrod.js文件定义的
            'target': 'new',
            'name': '修改用户密码',
        }

    @api.model
    def action_get_hrinfo(self, uid):
        view_id = self.env.ref('agrihrm.personal_view_hr_form_info').id
        emp_obj = self.env['hr.employee'].search([('user_id', '=', uid)])
        if view_id and emp_obj.id:
            return {
                'type': 'ir.actions.act_window',
                'res_model': 'hr.employee',
                'view_type': 'form',
                'view_mode': 'form',
                'views': [(view_id, 'form')],
                'target': 'main',
                'res_id': emp_obj.id,
                'context': dict(self._context),
                'flags': {
                    'headless': False,
                },
            }
        else:
            raise UserError("请添加自己的员工信息!")

